package encaps.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TypeConverter {

    public static boolean isNumeric(String str) {
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static Double convertToDouble(String str) throws ConversionException {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        str = str.replace(',', '.');
        str = str.trim();
        if (isNumeric(str)) {
            Double a = Double.valueOf(str);
            return a;
        } else {
            throw new ConversionException("String " + str + " is not a number");
        }
    }

    public static Integer convertToInt(String str) throws ConversionException {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        str = str.replace(',', '.');
        str = str.trim();
        if (isNumeric(str)) {
            Integer a = Integer.valueOf(str);
            return a;
        } else {
            throw new ConversionException("String " + str + " is not a number");
        }
    }

    public static Long convertToLong(String str) throws ConversionException {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        str = str.replace(',', '.');
        str = str.trim();
        if (isNumeric(str)) {
            Long a = Long.valueOf(str);
            return a;
        } else {
            throw new ConversionException("String " + str + " is not a number");
        }
    }

    public static Date convertToDate(String str) throws ConversionException {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        Date date;
        String[] datestr = str.split(" ");
        try {
            if (datestr.length == 1) {
                SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                date = formatter.parse(str);
            } else if (datestr.length == 2) {
                SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                date = formatter.parse(str);
            } else {
                throw new ConversionException();
            }
        } catch (Exception ex) {
            throw new ConversionException("Wrong date format");
        }
        return date;
    }

    public static String convertToString(double d) {
        return Double.toString(d);
    }

    public static String convertToString(int a) {
        return Integer.toString(a);
    }

    public static String convertToString(Date date) throws ConversionException {
        if (date == null) {
            return null;
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            return formatter.format(date);
        } catch (Exception ex) {
            throw new ConversionException("Wrong date format");
        }
    }

    public static String convertToStringWithTime(Date date) throws ConversionException {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
            return formatter.format(date);
        } catch (Exception ex) {
            throw new ConversionException("Wrong date format");
        }
    }
}
